package com.lz.all;

/**
 * StrategyDemo..
 * 一、要素
 *  1.策略接口-或抽象类（Strategy）
 *  2.环境类（Context）
 *  3.具体的策略（ConcreteStrategy）
 *
 *
 * @author Lizhong
 * @date 2019/5/13
 */
public class StrategyDemo {
    public static void main(String[] args) {
        Strategy strategy01 = new Strategy01();
        Strategy strategy02 = new Strategy02();
        Context context = new Context(strategy01);
        context.thing();
        context.setStrategy(strategy02);
        context.thing();
        context.setStrategy(() -> System.out.println("策略三"));
        context.thing();
    }
}

/**
 * 策略接口，用来替换if-else
 */
@FunctionalInterface
interface Strategy {
    void doThing();
}

/**
 * 具体实现一
 */
class Strategy01 implements Strategy {
    @Override
    public void doThing() {
        System.out.println("我是策略一");
    }
}

/**
 * 具体实现二
 */
class Strategy02 implements Strategy {
    @Override
    public void doThing() {
        System.out.println("我是策略二");
    }
}

/**
 * 环境
 */
class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    void thing() {
        strategy.doThing();
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }
}